package com.inforich.web.controller.tourist;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Arrays;
import cn.hutool.core.bean.BeanUtil;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import com.inforich.common.annotation.Log;
import com.inforich.common.core.controller.BaseController;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.BusinessType;
import com.inforich.tourist.domain.Commodity;
import com.inforich.tourist.service.ICommodityService;
import com.inforich.common.utils.poi.ExcelUtil;

/**
 * 产品Controller
 *
 * @author hjm
 * @date 2022-10-22
 */
@RestController
@RequestMapping("/tourist/commodity")
public class CommodityController extends BaseController {

  @Autowired
  private ICommodityService commodityService;

  /**
   * 查询产品列表
   */
  @PreAuthorize("@ss.hasPermi('tourist:commodity:list')")
  @GetMapping("/list")
  public Page<Commodity> list(Page<Commodity> page, Commodity commodity) {
    Map<String, Object> map = beanToMap(commodity);
    commodityService.pageCommodity(page, map);
    return page;
  }


  /**
   * 导出产品列表
   */
  @PreAuthorize("@ss.hasPermi('tourist:commodity:export')")
  @Log(title = "产品", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, Commodity commodity) {
    Map<String, Object> map = beanToMap(commodity);
    List<Commodity> list = commodityService.listCommodity(map);
    ExcelUtil<Commodity> util = new ExcelUtil<Commodity>(Commodity.class);
    util.exportExcel(response, list, "产品数据");
  }

  /**
   * 获取产品详细信息
   */
  @PreAuthorize("@ss.hasPermi('tourist:commodity:query')")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") Long id) {
    return AjaxResult.success(commodityService.getById(id));
  }

  /**
   * 新增产品
   */
  @PreAuthorize("@ss.hasPermi('tourist:commodity:add')")
  @Log(title = "产品", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@Validated @RequestBody Commodity commodity) {
    return toAjax(commodityService.save(commodity));
  }

  /**
   * 修改产品
   */
  @PreAuthorize("@ss.hasPermi('tourist:commodity:edit')")
  @Log(title = "产品", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@Validated @RequestBody Commodity commodity) {
    return toAjax(commodityService.updateById(commodity));
  }

  /**
   * 删除产品
   */
  @PreAuthorize("@ss.hasPermi('tourist:commodity:remove')")
  @Log(title = "产品", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public AjaxResult remove(@PathVariable Long[] ids) {
    return toAjax(commodityService.removeByIds(Arrays.asList(ids)));
  }
}
